using System;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;

namespace AvaloniaApplication1.Views;

public partial class MainWindow : Window
{
    private const string Moving = ":moving";

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
    {
        base.OnApplyTemplate(e);

        var border = e.NameScope.Find<Border>("PART_Title");

        if (border == null)
            return;
        
        border.PointerPressed += Border_PointerPressed;
        border.PointerReleased += Border_PointerReleased;
    }

    private void Border_PointerReleased(object? sender, Avalonia.Input.PointerReleasedEventArgs e)
    {
       PseudoClasses.Set(Moving, false);
    }

    private void Border_PointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
    {
        this.BeginMoveDrag(e);
        PseudoClasses.Set(Moving, true);
    }

    private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
    {
        this.BeginMoveDrag(e);
    }
}